package edu.towson.cosc.cosc455.interfaces.twellener;

import edu.towson.cosc.cosc455.interfaces.LexicalAnalyzer;

public class mylexicalAnalyzer implements LexicalAnalyzer {

	String input;
	int charat;
	@Override
	public void giveinput(String passed)
	{
		input=passed;
		//System.out.println("passed"+input);
		charat=0;
	}
	public String getNextToken() {
		String TempToken="";
		//checks if document is done
		if(charat>=input.length())
		{
			
			return "";
		}
		//strips white space
		while( charat< input.length()&&(input.charAt(charat) == ' '||input.charAt(charat)=='\n'||input.charAt(charat)=='	'))
		{
	
			charat++;
		}
		//if white space was the end of the document it is over
		if(charat>=input.length())
		{
			
			return "";
		}
		//Checks to make sure that a tag is valid if tag is not valid program prints error and terminates
		if(input.charAt(charat)=='#')
		{
			if((input.length()-charat)>14&&input.substring(charat, charat+15).equals("#START DOCUMENT"))
			{
				TempToken="#START DOCUMENT";
				charat+=15;
			}
			else if(input.length()-charat>12&&input.substring(charat, charat+13).equals("#END DOCUMENT"))
			{
				TempToken="#END DOCUMENT";
				charat+=13;
			}
			else if(input.length()-charat>10&&input.substring(charat, charat+11).equals("#START HEAD"))
			{
				TempToken="#START HEAD";
				charat+=11;
			}
			else if(input.length()-charat>8&&input.substring(charat, charat+9).equals("#END HEAD"))
			{
				TempToken="#END HEAD";
				charat+=9;
			}
			else if(input.length()-charat>11&&input.substring(charat, charat+12).equals("#START TITLE"))
			{
				TempToken="#START TITLE";
				charat+=12;
			}
			else if(input.length()-charat>9&&input.substring(charat, charat+10).equals("#END TITLE"))
			{
				TempToken="#END TITLE";
				charat+=10;
			}
			else if(input.length()-charat>15&&input.substring(charat, charat+16).equals("#START PARAGRAPH"))
			{
				TempToken="#START PARAGRAPH";
				charat+=16;
			}
			else if(input.length()-charat>13&&input.substring(charat, charat+14).equals("#END PARAGRAPH"))
			{
				TempToken="#END PARAGRAPH";
				charat+=14;
			}
			else if(input.length()-charat>10&&input.substring(charat, charat+11).equals("#START BOLD"))
			{
				TempToken="#START BOLD";
				charat+=11;
			}
			else if(input.length()-charat>8&&input.substring(charat, charat+9).equals("#END BOLD"))
			{
				TempToken="#END BOLD";
				charat+=9;
			}
			else if(input.length()-charat>13&&input.substring(charat, charat+14).equals("#START ITALICS"))
			{
				TempToken="#START ITALICS";
				charat+=14;
			}
			else if(input.length()-charat>11&&input.substring(charat, charat+12).equals("#END ITALICS"))
			{
				TempToken="#END ITALICS";
				charat+=12;
			}
			else if(input.length()-charat>10&&input.substring(charat, charat+11).equals("#START LIST"))
			{
				TempToken="#START LIST";
				charat+=11;
			}
			else if(input.length()-charat>8&&input.substring(charat, charat+9).equals("#END LIST"))
			{
				TempToken="#END LIST";
				charat+=9;
			}
			else if(input.length()-charat>10&&input.substring(charat, charat+11).equals("#START ITEM"))
			{
				TempToken="#START ITEM";
				charat+=11;
			}
			else if(input.length()-charat>8&&input.substring(charat, charat+9).equals("#END ITEM"))
			{
				TempToken="#END ITEM";
				charat+=9;
			}
			else if(input.length()-charat>7&&input.substring(charat, charat+8).equals("#NEWLINE"))
			{
				TempToken="#NEWLINE";
				charat+=8;
			}
			else if(input.length()-charat>11&&input.substring(charat, charat+12).equals("#START AUDIO"))
			{
				TempToken="#START AUDIO";
				charat+=12;
			}
			else if(input.length()-charat>9&&input.substring(charat, charat+10).equals("#END AUDIO"))
			{
				TempToken="#END AUDIO";
				charat+=10;
			}
			else if(input.length()-charat>11&&input.substring(charat, charat+12).equals("#START VIDEO"))
			{
				TempToken="#START VIDEO";
				charat+=12;
			}
			else if(input.length()-charat>9&&input.substring(charat, charat+10).equals("#END VIDEO"))
			{
				TempToken="#END VIDEO";
				charat+=10;
			}
			else if(input.length()-charat>12&&input.substring(charat, charat+13).equals("#START DEFINE"))
			{
				TempToken="#START DEFINE";
				charat+=13;
			}
			else if(input.length()-charat>10&&input.substring(charat, charat+11).equals("#END DEFINE"))
			{
				TempToken="#END DEFINE";
				charat+=11;
			}
			else if(input.length()-charat>4&&input.substring(charat, charat+5).equals("#NAME"))
			{
				TempToken="#NAME";
				charat+=5;
			}
			else if(input.length()-charat>5&&input.substring(charat, charat+6).equals("#VALUE"))
			{
				TempToken="#VALUE";
				charat+=6;
			}
			else if(input.length()-charat>9&&input.substring(charat, charat+10).equals("#START USE"))
			{
				TempToken="#START USE";
				charat+=10;
			}
			else if(input.length()-charat>7&&input.substring(charat, charat+8).equals("#END USE"))
			{
				TempToken="#END USE";
				charat+=8;
			}
			else
			{
				System.out.println("# was detected with out being followed by a valid tag lexical error program will now terminate");
				System.exit(0);
			}
			
			
		}
		else
		{
			//checks for valid character in strings
			while((input.charAt(charat)!='#') && (charat< input.length()))
			{
				
				//checks to make sure characters are valid in strings
				if((input.charAt(charat)>='A'&&input.charAt(charat)<='Z')||((input.charAt(charat)>='a')&&(input.charAt(charat)<='z'))||(input.charAt(charat)==' ')||(input.charAt(charat)=='?')
						||(input.charAt(charat)==',')||(input.charAt(charat)=='.')||(input.charAt(charat)=='"')||(input.charAt(charat)=='!')||(input.charAt(charat)==':')
						||(input.charAt(charat)=='/')||(input.charAt(charat)=='%')||(input.charAt(charat)>='0'&&input.charAt(charat)<='9')||(input.charAt(charat)=='	'))
				{
					
					TempToken=TempToken+input.charAt(charat);
					
					charat++;
				}
			}
		}
		return TempToken;
		
	}

	@Override
	public void getCharacter() {
		// TODO Auto-generated method stub

	}

	@Override
	public void addCharacter() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isSpace(String c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean lookupToken() {
		// TODO Auto-generated method stub
		return false;
	}

}
